<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总记录{{ totoal }}条</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">导入excel</el-button>
          <el-button
            v-loading.fullscreen.lock="fullscreenLoading"
            type="danger"
            size="small"
            element-loading-text="导出中"
            @click="Export"
          >
            导出excel</el-button>
          <el-button type="primary" size="small" @click="addFn">新增员工</el-button>
        </template>
      </page-tools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{prop:'workNumber'}">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式" prop="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable width="102" />
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template slot-scope="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="delFn(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            :total="totoal"
            :page-size="params.size"
            layout="prev, pager, next"
            @current-change="PageFn"
          />
        </div>
      </el-card>
      <el-dialog
        :visible.sync="showDialog"
        title="添加"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hCloseDialog"
      >
        <addEmloyees ref="remve" @close="showDialog=false" @Refres="loadEmpList" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addEmloyees from './addEmloyees'
import { reqGetEmployeeList, delEmployee } from '@/api/employees'
export default {
  components: {
    addEmloyees
  },
  data() {
    return {
      fullscreenLoading: false,
      // 控制遮罩层隐藏显示
      showDialog: false,
      // 渲染页面数组
      list: [],
      // 总条数
      totoal: 0,
      params: {
        page: 1,
        size: 5
      }
    }
  },
  created() {
    this.loadEmpList()
  },
  methods: {
    // 铺设员工页面
    async loadEmpList() {
      const res = await reqGetEmployeeList(this.params)
      this.list = res.data.data.rows
      this.totoal = res.data.data.total
      // 后台返回1修改为正式
      this.list.forEach((item) => {
        const name = item.formOfEmployment === 1 ? '正式' : '非正式'
        item.formOfEmployment = name
      })
    },
    // 换页功能
    PageFn(curr) {
      // 页码给到page
      this.params.page = curr
      // 刷新页面
      this.loadEmpList()
    },
    // 实现删除调用删除员工函数
    delFn(id) {
      this.$confirm('确定要删除吗？').then(() => {
        this.Dodel(id)
        this.loadEmpList()
        if (this.list.length - 1 === 0) {
          this.params.page--
        }
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(error => {
        console.log(error)
      })
    },
    // 删除员工接口
    async Dodel(id) {
      const res = await delEmployee(id)
      console.log(res)
    },
    // 添加实现弹出层关闭
    addFn() {
      this.showDialog = true
    },
    // 点击添加或删除时清空from表单里面的数据
    hCloseDialog() {
      this.$refs.remve.remove()
      console.log(this.$refs.remve.remove)
    },
    // 导出
    Export() {
      this.fullscreenLoading = true
      import('@/vendor/Export2Excel').then(async excel => {
        // excel表示导入的模块对象
        const res = await reqGetEmployeeList(this.params)
        const arr = res.data.data.rows
        console.log(arr)
        const { header, data } = this.exportApi(arr)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
        setTimeout(() => {
          this.fullscreenLoading = false
        }, 4000)
      })
    },
    exportApi(arr) {
      const map = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门'
      }
      arr.forEach(item => {
        const name = item.formOfEmployment === 1 ? '正式' : '非正式'
        item.formOfEmployment = name
      })
      const fister = arr[0]
      const header = Object.keys(fister).map(item => map[item])
      const data = arr.map(item => {
        return Object.values(item)
      })
      return { header, data }
    }
  }
}
</script>
